axios.interceptors.request.use(
    (config)=>{
        config.headers['Authorization']='Bearer'+''+localStorage.getItem('token');
        return config
    },
    (error)=>{
        return Promise.reject(error)
    }
)

axios.interceptors.response.use(
    (res)=>{
        return res
    },
    (error)=>{
        if(error.response.status==401){
            localStorage.removeItem('token')
            location.href='./login.html'
        }else if(error.response.status==404){
            alert('访问路径有误')
        }else if(error.response.status==500){
            alert('服务器内部错误')
        }
        return Promise.reject(error)
    }
)

var searchBtn=document.querySelector('.sousuo button')
var inputEle=document.querySelector('.sousuo input')

var userinfo=JSON.parse(localStorage.getItem('userinfo'))

document.querySelector('.username').innerHTML=userinfo.name



searchBtn.onclick=()=>{
    axios.get('/user/search',{params:{searchkey:inputEle.value}}).then((res)=>{
        render(res.data.list)
        console.log(res.data);
    })
}

var tablebody=document.querySelector('.table-body')
tablebody.onclick=(e)=>{
   if(e.target.tagName=='SPAN'&& e.target.classList.contains('icon-shanchu')) {
    // console.log(e.target.dataset.phone);
    axios.post('/user/delete',{phone:e.target.dataset.phone}).then((res)=>{
        // console.log(res.data);
        render(res.data.list)
    })
   }

}



function render(data){
    var str=''
    str=data.map((item,index)=>{
        return  `<ul class="row">
        <li>${item.email}</li> 
        <li>${item.name}</li>
        <li>${item.phone}</li>
        <li>${item.ingroup}</li>
        <li>${item.isadmin?'是':'否'}</li>
        <li class="${item.status?'':'error'}">${item.status?'正常':'异常'}</li>
        <li>
            <span class="iconfont icon-bianji editicon" id="101"></span>
            <span class="iconfont icon-shanchu deleteicon" data-phone="${item.phone}"></span>
        </li>
    </ul>
        `
    }).join('')
    if( str == '' ){
        str = '<ul style="text-align:center;color:red">未查询到数据!</ul>'
    }
    document.querySelector('.table .table-body').innerHTML=str
}

axios.get('user/list',{params: { page:1,count:2},headers:{}}).then((res)=>{
    render(res.data.list)
    console.log(res.data);
})

